package hibernateDAOS;

import hibernate.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.Comanda;
import beans.PlanProduccion;


public class PlanProduccionDAO 
{
	private static PlanProduccionDAO instancia = null;
	private static SessionFactory sf = null;
	
	public static PlanProduccionDAO getInstancia(){
		if(instancia == null)
		{
			sf = HibernateUtil.getSessionFactory();
			instancia = new PlanProduccionDAO();
		} 
		return instancia;
	}
	
	
	public  void guardarPlanProduccion(PlanProduccion pp){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(pp);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void eliminarPlanProduccion(PlanProduccion pp){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(pp);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarPlanProduccion(PlanProduccion pp){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(pp);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> obtenerPlanesProduccion(){
		Session session = sf.openSession();
		Query q = session.createQuery("from PlanProduccion");
		List<PlanProduccion> planesProd = q.list();
		
		Object[] o;
		List<Object[]> lista = new ArrayList<Object[]>();
		for(PlanProduccion pp : planesProd){
			o = new Object[2] ;
			o[0] = (Object) pp.getIdPlanProduccion();
			o[1] = (Object) pp.getCumplimietoTotal();
			lista.add(o);
		}
		
		session.close();
		return lista;
	}

}
